﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ThuVien;

/// <summary>
/// Summary description for PagePopUp
/// </summary>
public class PagePopUp : BasePage
{
	public PagePopUp()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    override protected void OnLoad(EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);

        if (this.Page.IsPostBack == false)
        {
            //create js for page popup
            this.CreateScriptForPopUpPage();

            //check page popup k dc danh vao web address de truy cap
            this.CheckPagePopUp();

            //get UserInfo from NoneSSL Site truyen qua
            //this.GetUserInfoFromNoneSSL();

        }

        //check Cookie
        //this.CheckDataCookie();

        if (this.UserInfo.UserISN == 0)
        {
            //chan khong cho danh dia chi cac trang popup vao web browser
            //RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("GotoPageLogin()");
            //ScriptManager.RegisterStartupScript(this.Page, typeof(this.Page), "LoadJS", "LoadJS();", true);
            //ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CheckUserLogin", "GotoPageLogin();", true);

            switch (FunctionsUtils.GetPageName().ToLower())
            {
                case "eventdetails.aspx":
                case "liveshow.aspx":
                    break;
                default:
                    //Response.Redirect("Login.aspx");
                    this.GoToLoginPageJS();
                    break;
            }
        }
        else
        {
            //RadAjaxManager.GetCurrent(Page).AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(RadAjaxManager_AjaxRequest);
        }

        base.OnLoad(e);
    }

    //protected virtual void RadAjaxManager_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
    //{

    //}

    private void CheckPagePopUp()
    {
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CheckPopUp", "CheckPopUp();", true);
    }

    private void CreateScriptForPopUpPage()
    {
        string s = "function CheckPopUp(){if (window.parent == null) location.href=\"Default.aspx\";}function GotoPageLogin(){window.parent.location.href=\"Default.aspx\";}";
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "sbjs", s, true);
    }

    private void GoToLoginPageJS()
    {
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "GotoPageLogin", "GotoPageLogin();", true);
    }

    protected void ClosePopup(bool refreshParent)
    {
        if (refreshParent)
        {
            Response.Write("<script>window.parent.CloseDialog(); window.parent.location.href = window.parent.location.href;</script>");
        }
        else
        {
            Response.Write("<script>window.parent.CloseDialog();</script>");
        }
    }

    protected void ClosePopup_CallMethod(string Method)
    {
        Response.Write("<script>window.parent." + Method.Trim(';') + "; window.parent.CloseDialog();</script>");
    }

    protected void ClosePopupScripManager(bool refreshParent)
    {
        if (refreshParent)
        {
            string script = "window.parent.CloseDialog(); window.parent.location.href = window.parent.location.href;";
            System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CloseDialog", script, true);
        }
        else
        {
            string script = "window.parent.CloseDialog();";
            System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CloseDialog", script, true);
        }
    }
    protected void ClosePopupScripManager_CallMethod(string Method)
    {
        string script = "window.parent." + Method.Trim(';') + "; window.parent.CloseDialog();";
        System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CloseDialog", script, true);
    }

/*
    private void GetUserInfoFromNoneSSL()
    {
        //if (this.Request.IsSecureConnection == true)
        //{
            int UserISN = FunctionsUtils.ConvertToInt(FunctionsUtils.DecryptWeb(Request["isn"]), 0);
            string UserName = FunctionsUtils.ConvertToString(FunctionsUtils.DecryptWeb(Request["us"]), string.Empty);
            if (UserISN > 0)
            {
                User user = new User();
                user.SetUserInfoByISN(UserISN);

                if (user.UserName.Equals(UserName) == true)
                {
                    //create cookie cho site o SSL chi co tac dung khi browser open
                    this.CreateCookieForSSL(user.Email, user.Password);

                    //HttpContext.Current.Session["UserInfo"] = user;
                    this.UserInfo = user;
                }
            }
            else
            {
                this.UserInfo = null;
            }
        //}
    }
 */
}